package com.demo.sp;

import com.demo.common.model.*;

import java.util.List;

public class SpService {

    private FUser fUserDao = new FUser().dao();
    private Flow flowDao = new Flow().dao();
    private FlowNode flowNodeDao = new FlowNode().dao();

    /**
     * 用户列表
     *
     * @return
     */
    public List getUsers() {
        return fUserDao.findAll();
    }

    /**
     * 查询所有表单
     *
     * @return
     */
    public List<Flow> getAllForm() {
        return (flowDao.find("select id, name from flow"));
    }


    /**
     * \
     * 查询审批节点  从第一个开始
     * i  第n节点
     */
    public String getNodeUid(int flowid, int node) {
        FlowNode flowNode = flowNodeDao.findFirst("select uid, uname from flow_node where flowid=? and node=?", flowid, node);
        if (flowNode != null) {
            return flowNode.getUid();
        } else {
            return null;
        }
    }
    /**
     * \
     * 查询审批节点  从第一个开始
     * i  第n节点
     */
    public String getBy(int id) {
        FlowNode flowNode = flowNodeDao.findFirst("select uid, uname from flow_node where id = ?", id);
        if (flowNode != null) {
            return flowNode.getUid();
        } else {
            return null;
        }
    }

    /**
     * 根据id查询用户审批流程
     * @param uid
     * @return
     */
    public List<Schedule> findMyflow(String uid) {

        List<Schedule> schedules = new Schedule().find("select id,f_id,f_name,uid,html from schedule where uid = ?", uid);
        if (schedules != null) {
            return schedules;
        } else {
            return null;
        }
    }

    /**
     * 查询我审核的节点流程
     * @param uid
     * @return
     */
    public List<Schedule> isMyflow(String uid) {
        List<Schedule> schedules = new Schedule().find("select id,node_uid,f_id,f_name,uid,state,html from schedule where node_uid = ? or uid= ?", uid, uid);
        if (schedules != null) {
            return schedules;
        } else {
            return null;
        }
    }

    public void goNextNode(int id,String NextNodeUid) {

        Schedule schedules = new Schedule().findFirst("select id,node_uid,f_id,f_name,uid,html from schedule where id = ?",id);
        schedules.setNodeUid(NextNodeUid);
        schedules.update();
    }

    public void flowSetMsg() {

    }

    public List<ScheduleMsg> findMsg(Integer id) {
        List<ScheduleMsg> scheduleMsgs = new ScheduleMsg().find("select id,sid,uid,msg,msgtime from schedule_msg where sid = ? ", id);
        return scheduleMsgs;
    }

    public Schedule findByid(Integer sid) {
        Schedule schedule = new Schedule().findById(sid);
        List<ScheduleMsg> scheduleMsgList = new ScheduleMsg().find("select id,sid,uid,msg,msgtime from schedule_msg where sid = ? order by msgtime desc",sid);
        List<ScheduleMsg> lis = null;
        for (ScheduleMsg scheduleMsg:scheduleMsgList){
            FUser fuser = new FUser().findById(scheduleMsg.getUid());
            scheduleMsg.put("uname",fuser.getUname());
        }
        int i =0;
        //自己
        FlowNode first = new FlowNode().findFirst("select id,flowid,node,uid from flow_node where flowid = ? and uid = ?", schedule.getFId(), schedule.getNodeUid());
        //下一个
        FlowNode first1 = new FlowNode().findFirst("select id,flowid,node,uid,uname from flow_node where node = ? and flowid = ?", first.getNode() + 1, schedule.getFId());
        //上一个
        FlowNode first2 = new FlowNode().findFirst("select id,flowid,node,uid,uname from flow_node where node = ? and flowid = ?", first.getNode() - 1, schedule.getFId());
        /**
         * 1没有下一步 2没有上一不 3 上下都有人
         */
        if (first1==null){
            i  = 1;
        }else if (first2==null){
            i =  2;
        }else {
            i =  3;
        }
        schedule.put("istrue",i);
        schedule.put("nodeid",first.getNode());
        schedule.put("msgList",scheduleMsgList);
        return schedule;
    }

    public List<FlowNode> FindFlowNode(Integer fid) {
        List<FlowNode> flowNodes = new FlowNode().find("select id,flowid,node,uid,uname from flow_node where flowid = ? order by node", fid);
        return flowNodes;
    }

    public List<FlowNode> findFlowNode( Integer fid,Integer uid) {
        List<FlowNode> flowNodes = new FlowNode().find("select id,flowid,node,uid,uname from flow_node where flowid = ?", fid);
        return flowNodes;
    }

    public Schedule findScheduleByUidAndFid(Integer userid, Integer flowid) {
        //根据用户id和审批项id查询出数据
        Schedule schedule = new Schedule().findFirst("select id,node_uid,f_id,f_name,uid from schedule where uid=? and f_id = ?", userid, flowid);
        return schedule;
    }

    public FlowNode findNodeByUidandFid(String nodeUid, Integer flowid) {
        //查出他的节点对象
        FlowNode flowNode = new FlowNode().findFirst("select id,flowid,node,uid,uname from flow_node where uid = ? and flowid = ?",nodeUid, flowid);
        return flowNode;
    }

    public FlowNode findNext(int i, Integer flowid) {
        //查出下一个对象
        FlowNode first = new FlowNode().findFirst("select id,flowid,node,uid,uname from flow_node where node = ? and flowid = ?", i, flowid);
        return first;
    }

    public Schedule findScheduleBySid(String sid) {
        Schedule schedule = new Schedule().findFirst("select id,node_uid,f_id,f_name,uid,html from schedule where id = ?", sid);
        return schedule;
    }

    //查询FlowNode单个对象
    public FlowNode findFlowNodeBySidAndUid(Integer fid, Integer uid) {
        FlowNode first = new FlowNode().findFirst("select  id,flowid,node,uid,uname from flow_node where flowid = ? and  uid = ?", fid, uid);
        return first;
    }

    public List<FlowNode> findBeforeFlowNode(FlowNode flowNode) {
        List<FlowNode> flowNodes = new FlowNode().find("select id,flowid,node,uid,uname from flow_node where flowid = ? and node between ? and ?", flowNode.getFlowid(), 1, flowNode.getNode() - 1);
        return flowNodes;
    }
}
